//
//  RIODiscoverSeachView.swift
//  RIOWeiBo
//
//  Created by Rio on 16/4/7.
//  Copyright © 2016年 RIO. All rights reserved.
//

import UIKit

class RIODiscoverSeachView: UIView {
    
    @IBOutlet weak var cancelButton: UIButton!
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var rightLayoutConstraint: NSLayoutConstraint!
    /**
     对xib的具体设置
     */
    
    override func awakeFromNib() {
        
        frame = CGRect (x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: bounds.height)
        
        /**
         设置leftview
         */
        textField.leftView = UIImageView(image:UIImage(named: "searchbar_textfield_search_icon"))
        
        textField.leftView?.frame = CGRect (x: 0, y: 0, width: bounds.height, height: bounds.height)
        /**
         设置图片显示模式
         */
        textField.leftView?.contentMode = .Center
        /**
         让leftview可见
         */
        textField.leftViewMode = .Always
        
        /**
         设置输入框是圆角
         */
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        
        
    }
    
    /**
     载入xib
     
     - returns: discover界面的搜索框
     */

    class func loadSeachView() -> RIODiscoverSeachView {
        
        let seachView = NSBundle.mainBundle().loadNibNamed("RIODiscoverSeachView", owner:nil, options: nil).last as! RIODiscoverSeachView
        
        
        
       return seachView
    }
    
    /**
     点击取消按钮让文本框重新盖住按钮,并且收回键盘
     */
    @IBAction func cancelBtnDidClick() {
        
        /**
         取消键盘的第一响应者
         */
        textField.resignFirstResponder()
        /**
         *  修改约束,刷新页面
         */
        rightLayoutConstraint.constant = 0
        UIView.animateWithDuration(0.25) {
            self.layoutIfNeeded()
        }
    }
    
    
    /**
     当点击文本框时,改变右边约束让取消按钮显示出来
     */
    
    @IBAction func eidtingDidBegin(sender: AnyObject) {
        
        /**
         *  修改约束
         */
        rightLayoutConstraint.constant = cancelButton.frame.width + 20
        /**
         *  在动画闭包中刷新页面
         */
        UIView.animateWithDuration(0.25) {
            self.layoutIfNeeded()
        }
        
    }

}
